package com.dff.jrfw.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dff.jrfw.R;

import java.text.MessageFormat;
import java.util.Timer;
import java.util.TimerTask;


public class ResetLoginPsdSuccessDialog extends Dialog {

    private int second = 3;
    private Timer timer;
    private TimerTask task;
    private TextView tvHint2;
    public ResetLoginPsdSuccessDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
        setCanceledOnTouchOutside(false);   // 点击其他地方dialog不消失
        setCancelable(false);         // 点击返回键dialog不消失
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_reset_login_password_success);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        findViewById(R.id.btn_back).setOnClickListener(v -> dismiss());
        tvHint2 = findViewById(R.id.tv_hint2);
    }

    @Override
    public void show() {
        super.show();
        clearCutDown();
        task = new TimerTask() {
            @Override
            public void run() {
                second--;
                if (second > 0) {
                    tvHint2.post(() -> tvHint2.setText(MessageFormat.format("{0}秒后自动返回登录首页", second)));
                } else {
                    dismiss();
                }
            }
        };
        timer = new Timer();
        timer.schedule(task, 1000, 1000);
    }

    @Override
    public void dismiss() {
        clearCutDown();
        super.dismiss();
    }

    private void  clearCutDown(){
        if (task != null) {
            task.cancel();
            task = null;
        }
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}
